<template>
	<el-dialog :close-on-click-modal="false" :title="mainTitleName()" v-model="visible" :width="800" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px" label-position="right" v-loading.fullscreen="loading">
			<el-row>
				<el-col :span="24">
					<el-form-item label="标题" prop="title">
						<el-input v-model="form.title" placeholder="请输入章节标题" :maxlength="100" clearable
							show-word-limit></el-input>
					</el-form-item>
					<el-form-item label="状态" prop="status">
                        <el-radio-group v-model="form.status">
                            <el-radio :label="1">启用</el-radio>
                            <el-radio :label="0">禁用</el-radio>
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item label="排序号" prop="sort">
						<el-input v-model="form.sort" placeholder="请输入排序号"></el-input>
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>

</template>

<script>
export default {
	emits: ['success', 'closed'],
    components: {
    },
	props: {
		mainName: {
			type: String,
			default: () => {
				return '阅读文章'
			}
		},
	},
	data() {
		return {
			mode: "add",
			course: null,
            loading: false,
			visible: false,
			isSaveing: false,
			form: this.getDefaultRuleForm(),
			rules: {
				title: [
					{required: true, message: '请输入标题'}
				],
                sort: [
                    {
                        required: false,
                        validator: (rule, value, callback) => {
                            var myreg = /^(-|[0-9])\d*$/;
                            if (value && !myreg.exec(value)) {
                                callback(new Error('只能输入整数'))
                            } else {
                                callback()
                            }
                        }
                    },
                ]
			},
            play: {
                src: '',
                type: 1,
                status: false
            },
            voiceFiles: [],
            translationVoiceFiles: []
		}
	},
	mounted() {

	},
	methods: {
		//显示
		open(mode = 'add', course) {
			this.mode = mode;
			this.visible = true;
			this.course = course
            this.form.courseId = course.id
			return this
		},
        phraseAdd(arr) {
			arr.push({
                    content: '',
                    translate: ''
				}
			)
		},
        phraseSub(arr, index) {
			arr.splice(index, 1)
		},
		//表单提交方法
		async submit() {
			if (!await this.$refs.dialogForm.validate().catch(() => { return false })) {
				return false
			}

            let _data = {}

            for (let key in this.form) {
                _data[key] = this.form[key]
            }

			try {
				this.isSaveing = true;
				if (this.form.id) {
					await this.$API.courseArticle.sentenceaction.put(_data)
				} else {
					await this.$API.courseArticle.sentenceaction.post(_data)
				}

				this.$emit('success')
				this.visible = false;
				this.$notify.success("保存成功")
			} finally {
				this.isSaveing = false;
			}
		},
		//表单注入数据
		async setData(row) {
            this.loading = true
			await this.$API.courseArticle.sentenceaction.get({id: row.id}).then(res => {
                this.loading = false
                let _data = res.data
                let _form = {
                    id: _data.id,
                    title: _data.title,
                    courseId: _data.courseId ? _data.courseId : '',
                    status: _data.status,
                    sort: _data.sort
                }
                this.form = _form
            }).finally(() => {
				this.loading = false
			})
        },
		getDefaultRuleForm() {
			return {
				id: null,
				title: '',
				courseId: '',
				status: 1,
                sort: '',
			}
		},
		mainTitleName() {
			let titleName = '';
            let mainName = '练习章节'
			if (!this.form.id && this.course) {
				titleName = `添加【${this.course.name}】${mainName}`;
			} else if (!this.form.id) {
				titleName = `新增${mainName}`;
			} else {
				titleName = `编辑${mainName}`;
			}
			return titleName;
		},
	}
}
</script>

<style>
</style>
